<template>
  <div class="app-container">
    <!-- <aside>
      The guide page is useful for some people who entered the project for the first time. You can briefly introduce the
      features of the project. Demo is based on
      <a href="https://github.com/kamranahmedse/driver.js" target="_blank">driver.js.</a>
    </aside>
    <el-button icon="el-icon-question" type="primary" @click.prevent.stop="guide">
      Show Guide
    </el-button>-->
    <div class="head">
      <div class="head_left">
        <!-- <el-button @click="addStaffClick">新增员工</el-button> -->
        <el-dialog
          :title="titleType"
          :visible.sync="dialogVisible"
          class="dialogStyle"
        >
          <div class="box_input_one">
            <div>姓名:</div>
            <div class="divMargin">
              <el-input
                v-model="info.integral_name"
                type="text"
                :disabled="true"
                class="dialogInputClass"
              />
            </div>
            <div class="divMargin">性别:</div>
            <div class="divMargin styleMagin">
              <el-input
                v-model="info.integral_sex"
                type="text"
                :disabled="true"
                class="dialogInputClass"
              />
            </div>
            <div class="divMargin">来源:</div>
            <div class="divMargin">
              <el-input
                v-model="info.integral_source"
                type="text"
                style="color: red"
                :disabled="true"
                class="dialogInputClass"
              />
            </div>
          </div>

          <div class="box_input_two">
            <div>规则:</div>
            <div class="divMargin">
              <el-input
                v-model="info.integral_department"
                type="text"
                oninput="value=value.replace(/[^\d]/g,'')"
                maxlength="11"
                class="dialogInputClass"
                :disabled="true"
              />
            </div>

            <div class="divMargin" style="width: 30px" />

            <div class="divMargin">积分:</div>
            <div class="divMargin styleMagin">
              <el-input
                v-model="info.integral"
                type="text"
                :disabled="true"
                class="dialogInputClass"
              />
            </div>
          </div>
          <div class="box_input_two">
            <div>所属部门:</div>
            <div class="divMargin">
              <el-input
                v-model="info.integral_department"
                type="text"
                oninput="value=value.replace(/[^\d]/g,'')"
                maxlength="11"
                class="dialogInputClass"
                :disabled="true"
              />
            </div>
            <div class="divMargin">打分时间:</div>
            <div class="divMargin">
              <el-date-picker
                v-model="info.integral_date"
                type="date"
                :disabled="true"
                placeholder="选择日期"
                format="yyyy-MM-dd"
              />
            </div>
          </div>

          <div class="box_input_three">
            <div>打分原因:</div>
            <div class="divMargin">
              <el-input
                v-model="info.reason"
                type="textarea"
                row="6"
                :disabled="true"
                class="dialogInputClass addressInput"
              />
            </div>
          </div>

          <div slot="footer" class="dialog-footer">
            <el-button @click="addStaffCancel">取 消</el-button>
            <el-button type="primary" @click="addStaffConfrim">确 定</el-button>
          </div>
        </el-dialog>
      </div>
      <div class="head_right">
        <div class="box_search">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
          <div>
            <el-button @click="selectClick">查询</el-button>
          </div>
        </div>
      </div>
    </div>

    <el-table
      :data="tableData"
      style="width: 100%"
      :default-sort="{ prop: 'date', order: 'desce nding' }"
    >
      <el-table-column
        prop="integral_name"
        label="姓名"
        width="100"
      />
      <el-table-column
        prop="integral_sex"
        label="性别"
        width="50"
      />
      <el-table-column
        prop="integral_department"
        label="所属部门"
        width="120"
      />
      <el-table-column prop="integral_source" label="来源" width="100">
        <template slot-scope="scope">
          <span
            v-if="
              scope.row.integral_source =='日报'
            "
            style="color: blue"
          >{{ scope.row.integral_source }}</span>
          <span v-else>{{ scope.row.integral_source }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="rule" label="规则" width="100" />
      <el-table-column
        prop="integral"
        label="积分"
        sortable
        width="100"
      />
      <el-table-column
        prop="integral_date"
        label="打分时间"
        width="160"
      />
      <el-table-column prop="reason" label="打分原因" />
      <el-table-column fixed="right" label="操作" width="119">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleEdit(scope.$index, scope.row)"
          >查看</el-button>
          <el-button
            type="text"
            size="small"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        ref="pager1"
        background
        :current-page="pageIndex"
        :page-size="pageSize"
        :total="total"
        layout="prev, pager, next"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
      />
    </div>
  </div>
</template>

<script>
import Driver from 'driver.js' // import driver.js
import 'driver.js/dist/driver.min.css' // import driver.js css
import steps from './steps'
import {
  getIntegralLog,
  getIntegralPage,
  deleteIntegralLog,
  searchIntegralLog
} from '@/api/integral'

export default {
  name: 'Guide',
  data() {
    return {
      titleType: '新增员工', // 动态修改dialog框的标题
      driver: null,
      search: '',
      pageIndex: 1,
      pageSize: 2,
      total: 0,
      // 人员信息
      info: {
        id: '',
        integral_name: '',
        integral_sex: '',
        integral_source: '',
        rule: '',
        integral: '',
        integral_department: '', // 部门
        integral_date: '',
        reason: ''
      },
      dialogVisible: false,
      tableData: []
    }
  },
  mounted() {
    this.driver = new Driver()
  },
  // 创建页面时，查询数据 渲染到组件
  created() {
    this.selectIntegralLog()
  },
  methods: {
    // 查询信息
    selectIntegralLog() {
      return new Promise((resolve, reject) => {
        getIntegralLog().then((response) => {
          this.tableData = response.data.data
          this.total = response.data.total // 查询的数据量,做分页处理
          this.pageSize = response.data.to
        })
      })
    },
    // 下一页
    sizeChangeHandle: function(size) {
      this.pagesize = size
      // console.log("size" + this.pageSize);
    },
    currentChangeHandle: function(val) {
      return new Promise((resolve, reject) => {
        getIntegralPage(val).then((response) => {
          this.tableData = response.data.data
        })
      })
    },
    // 编辑信息
    handleEdit($index, row) {
      this.titleType = '查看'
      this.info = Object.assign({}, row)
      // console.log(this.info);
      if (this.info.integral_source) {
        this.dialogVisible = true
      }
    },
    // 删除信息
    handleDelete($index, row) {
      this.$confirm('确认删除?', '警告', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          new Promise((resolve, reject) => {
            deleteIntegralLog(row).then((response) => {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              location.reload()
            })
          })
        })
        .catch((err) => {
          console.error(err)
        })
    },

    guide() {
      this.driver.defineSteps(steps)
      this.driver.start()
    },
    addStaffClick() {
      this.titleType = '新增员工'
      this.dialogVisible = true
    },
    // dialog的确认事件
    addStaffConfrim() {
      if (this.titleType == '新增员工') {
        // console.log("新增");
        this.dialogVisible = false
        return new Promise((resolve, reject) => {
          addUserInfo(this.info).then((response) => {
            this.dialogVisible = false
            this.$message({
              message: '添加成功',
              type: 'success'
            })
            location.reload()
          })
        })
      } else {
        new Promise((resolve, reject) => {
          updateUserInfo(this.info).then((response) => {
            this.dialogVisible = false
            this.$message({
              message: '修改成功',
              type: 'success'
            })
            // location.reload()
          })
        })
      }
    },
    // dialog的取消事件
    addStaffCancel() {
      this.dialogVisible = false
      this.info = ''
    },

    filterHandler() {},
    // 根据条件查询信息
    selectClick() {
      // console.log("查询的条件");
      // console.log(this.search);
      var arr = this
      return new Promise((resolve, reject) => {
        searchIntegralLog({ search: this.search }).then((response) => {
          // console.log("查询成功");
          // console.log({ search: this.search });
          this.tableData = response.data.data
          this.total = response.data.total
          this.pageSize = response.data.to
          this.search = ''
          // console.log(arr.tableData);
          // console.log(this.tableData);
        })
      })
    },
    formatDate(row, column) {
      // 获取单元格数据
      const data = row[column.property]
      if (data == null) {
        return null
      }
      // 对日期单元格进行简单的处理
      const dt = new Date(data)
      return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate()
    }
  }
}
</script>

<style >
.app-container th {
  text-align: center !important;
}
.app-container td {
  text-align: center !important;
}
.app-container .btn .btnFooter {
  /* border: 1px solid red; */
  margin-top: 5px;
}
.app-container .el-input__inner {
  width: 250px;
  height: 30px;
  margin-right: 10px;
}
.app-container .head {
  display: flex;
  flex-direction: row;
  width: 100%;
  line-height: 40px;
  height: 40px;
  align-items: center;
}
.app-container .head .head_left {
  width: 40%;
}
.app-container .head .head_right {
  width: 60%;
  display: flex;
  flex-direction: row;
}
.app-container .head .title {
  width: 55px;
  color: #5f5d5d;
}
.app-container .head .box_search {
  display: flex;
  flex-direction: row;
}
.app-container .head .head_left .el-button {
  width: 100px;
  height: 30px;
  text-align: center;
  line-height: 8px;
}
.app-container .head .head_right .el-button {
  width: 80px;
  height: 30px;
  text-align: center;
  line-height: 8px;
}
.app-container .head .box_input_one {
  display: flex;
  flex-direction: row;
}
.app-container .head .box_input_one .el-input__inner {
  width: 129px;
}
.app-container .head .box_input_one .divMargin {
  margin-left: 10px;
}
.app-container .head .box_input_two {
  display: flex;
  flex-direction: row;
}
.app-container .head .box_input_two .el-input__inner {
  width: 180px;
}
.app-container .head .box_input_two .divMargin {
  margin-left: 10px;
}
.el-radio--medium.is-bordered {
  height: 30px;
  padding: 8px 10px 0px 5px;
  margin: 0;
}
.addressInput .el-input__inner {
  width: 300px;
}
/* .box_input_three{
    display: flex;
  flex-direction: row;
} */
.box_input_three .dialogInputClass .el-textarea__inner {
  width: 550px;
  background-color: white;
  color: #000;
  opacity: 0.7;
}
</style>
